<template>
    <div class="common-page page-system-dapts" v-loading.fullscreen="loading">
        <div class="common-page__head">
            <!-- <comp-paths class="route" :paths="['/system', $route.fullPath]"></comp-paths> -->
            <div class="title">{{ $options.title }}</div>
        </div>
        <comp-dapts class="common-page__body" ref="list" :level="2" select link>
            <template #head="scope">
                <el-button type="plain" size="small" icon="el-icon-refresh" @click="search()">刷新列表</el-button>
                <el-button type="primary" size="small" icon="el-icon-plus" @click="addDapt()">添加行政单位</el-button>
                <el-button type="danger" size="small" icon="el-icon-delete" @click="delDapt(...scope.selects)">删除行政单位</el-button>
            </template>
            <template #body="scope">
                <el-button type="primary" size="mini" @click.stop="setDapt('start', scope.data)">编辑</el-button>
                <el-button type="danger" size="mini" @click.stop="delDapt(scope.data)">删除</el-button>
            </template>
        </comp-dapts>
        <comp-dialog :title="dapt && dapt.id ? '编辑行政单位' : '添加行政单位'" width="440px" ref="dapt">
            <el-form size="small" label-width="98px" @submit.native.prevent v-if="dapt">
                <el-form-item label="单位名称：" required>
                    <el-input ref="daptName" v-model="dapt.name" style="width: 320px" placeholder="请输入" :maxlength="100" :readonly="loading" @change="setDapt('validate')"></el-input>
                </el-form-item>
                <el-form-item label="所在城市：" required>
                    <el-tag style="margin: 0 10px 10px 0" disable-transitions v-if="dapt.dapt.id">{{ dapt.dapt.name }}</el-tag>
                    <div v-if="dapt.dapt.id"></div>
                    <el-button type="plain" icon="el-icon-plus" @click="setDapt('pick-dapt-start')">{{ dapt.dapt.id ? "更换城市" : "选择城市" }}</el-button>
                </el-form-item>
                <!-- <el-form-item label="排序值：">
                    <el-input v-model="dapt.sort" style="width: 160px" placeholder="请输入" :readonly="loading" @change="setDapt('validate')"></el-input>
                </el-form-item> -->
                <el-form-item>
                    <el-button type="primary" icon="el-icon-check" @click="setDapt('submit')">{{ dapt.id ? "保存" : "添加" }}</el-button>
                    <el-button type="plain" icon="el-icon-close" @click="setDapt('end')">放弃</el-button>
                </el-form-item>
            </el-form>
        </comp-dialog>
        <comp-dialog title="选择城市" width="60vw" height="80vh" ref="daptPicker">
            <comp-dapts style="height: 100%" ref="daptPickerList" :level="1" v-if="dapt">
                <template #body="scope">
                    <el-button type="primary" size="mini" icon="el-icon-check" @click.stop="setDapt('pick-dapt-end', scope.data)">选择</el-button>
                </template>
                <template #foot="scope">
                    <el-button type="danger" size="small" icon="el-icon-close" @click="setDapt('pick-dapt-end')">放弃</el-button>
                </template>
            </comp-dapts>
        </comp-dialog>
    </div>
</template>

<script>
    import mixin from "../mixin";
    import { string, int, deepCopy } from "../util";
    export default {
        name: "page-system-dapts",
        title: "行政单位管理",
        nodes: ["/menu/system"],
        childs: [],
        mixins: [mixin],
        data: () => ({
            loading: false,
            dapt: null,
        }),
        methods: {
            search() {
                if (this.loading) return;
                this.$nextTick(() => this.$refs.list.init());
            },
            addDapt() {
                if (this.loading) return;
                let dapt = this.plugins.user.formatDapt();
                this.setDapt("start", dapt);
            },
            setDapt(action, data) {
                if (this.loading) return;
                let form = this.dapt;
                if (!action || action === "start") {
                    this.dapt = deepCopy(data);
                    this.setDapt("validate");
                    this.$refs.dapt.init();
                    this.$nextTick(() => this.$refs.daptName.focus());
                } else if (action === "validate") {
                    let error = "";
                    form.name = string(form.name).trim();
                    error = error || (form.name ? "" : "请输入单位名称！");
                    error = error || (form.dapt.id ? "" : "请选择所在城市！");
                    form.level = 2;
                    form.sort = Math.max(int(form.sort), 0) || null;
                    if (data) this.plugins.modal.toast(error, "error");
                    return !error;
                } else if (action === "pick-dapt-start") {
                    this.$refs.daptPicker.init();
                    this.$nextTick(() => this.$refs.daptPickerList.init());
                } else if (action === "pick-dapt-end") {
                    this.$refs.daptPicker.close();
                    if (data) form.dapt = deepCopy(data);
                    this.setDapt("validate");
                } else if (action === "submit") {
                    if (!this.setDapt("validate", true)) return;
                    else this.loading = true;
                    if (form.id) this.plugins.user.setDapt(form).then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.toast("保存成功！", "success");
                            this.setDapt("end");
                            this.search();
                        } else {
                            this.plugins.modal.alert("保存失败", res.code + "：" + res.msg, "error");
                        }
                    });
                    else this.plugins.user.addDapt(form).then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.toast("添加成功！", "success");
                            this.setDapt("end");
                            this.search();
                        } else {
                            this.plugins.modal.alert("添加失败", res.code + "：" + res.msg, "error");
                        }
                    });
                } else if (action === "end") {
                    this.$refs.dapt.close();
                }
            },
            delDapt(...dapts) {
                if (this.loading) return;
                if (dapts.length === 0) return this.plugins.modal.toast("请先勾选要删除的行政单位哦！", "warning");
                let content = " <span style='color:red;'>" + (dapts.length > 1 ? "勾选的" + dapts.length + "个行政单位" : dapts[0].name) + "</span> 吗？";
                this.plugins.modal.alert("删除确认", "确定删除" + content, "warning", ["确定删除", "再想想"]).then((ok) => {
                    if (!ok) return;
                    this.loading = true;
                    this.plugins.user.delDapt(...dapts.reverse()).then((res) => {
                        this.loading = false;
                        if (res.code === 0) {
                            this.plugins.modal.toast("删除成功！", "success");
                            this.search();
                        } else {
                            this.plugins.modal.alert("删除失败", res.code + "：" + res.msg, "error");
                        }
                    });
                });
            },
            destroy() {
                if (this.loading) return;
                return true;
            },
            init() {
                if (this.loading) return;
                this.loading = true;
                this.$root.$refs.navbar.init();
                this.plugins.user.call(async () => {
                    let res = await this.plugins.user.init();
                    if (res.code === 0) { }
                    return res;
                }, async () => {
                    if (this.access(this.$route.name)) return { code: 0 };
                    return this.env.xhrErrors.find((error) => error.code === 4);
                }).then((res) => {
                    this.loading = false;
                    this.$root.$refs.navbar.init();
                    if (res.code === 0) {
                        this.search();
                    } else if (res.code === 2) {
                        this.$root.$refs.navbar.login();
                    } else {
                        this.plugins.modal.alert("页面加载失败", res.code + "：" + res.msg, "error").then(() => this.$router.replace("/home"));
                    }
                });
            },
        },
    };
</script>

<style lang="scss">
    .page-system-dapts {
        overflow: hidden;
    }
</style>